package cn.school.mybatis.servlet;


import cn.school.mybatis.Util.SpringMButil;
import cn.school.mybatis.pojo.Grade;
import cn.school.mybatis.service.GradeService;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/GradeServlet")
public class GradeServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        GradeService gradeService= SpringMButil.getBean(GradeService.class);
        List<Grade> list = gradeService.searchAll();

        String gradeJSON= JSON.toJSONString(list);
        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().print(gradeJSON);

        System.out.println(gradeJSON);
//        out.print(gradeJSON);
//        out.flush();
//        out.close();


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
